//
//  DeviceMonitorController.m
//  LazyHome
//
//  Created by yh on 2018/4/20.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "DeviceMonitorController.h"
#import "TCPClient.h"
#import <MJExtension/MJExtension.h>
#import "DeviceTCPManager.h"
@interface DeviceMonitorController ()

@end

@implementation DeviceMonitorController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCame:) name:NOTI_TCP_MESSAGE_CAME object:nil];
    // Do any additional setup after loading the view.
    NSError *error = nil;
    
    
   TCPClient *client =  [[DeviceTCPManager shareManager] creatClientWithHost:@"127.0.0.1" port:@"9999" localport:@"9999" error:&error];
    [client connectWithError:&error];
    
    
    NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//解决中文乱码
    NSDictionary *dict = @{@"cmd":@"1001",@"lights":@[
                                   @{
                                       @"deviceID":@"YHLOIKDD",
                                       @"lightState":@"0"
                                       }
                                   ]};
    
    //模拟设备连接上了
    dict = @{@"cmd":@"1001",@"deviceId":@"dsdf"};
    
    
    LHMessage *ms = [LHMessage messageWithData:[[dict mj_JSONString] dataUsingEncoding:strEncode] cmd:0x1001];
    [client sendData:[ms messageData] withTimeout:-1 tag:1001];
}

- (void)messageCame:(NSNotification *)noti{
    LHMessage *msg = noti.object;
    NSString *string =[[NSString alloc]initWithData:msg.data encoding:NSUTF8StringEncoding];
    NSLog(@"sss - %@",string);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)mkConnect:(id)sender {
    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
